<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\GasFee;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class GasFeeController extends Controller
{
    /*
      * 气费列表
      */
    public function index(Request $request, GasFee $gasFee)
    {
        $communityId = $request->input('community_id', 1);
        if (isset($request->condition) && !empty($request->condition)) {
            $res = $gasFee->indexPageLike($request->page, $request->limit, $request->condition, $communityId);
        } else {
            $res = $gasFee->indexPage($request->page, $request->limit, $communityId);
        }
        if ($res && count($res) > 0) {
            return $this->return_json(200, $res, '查询成功');
        } else {
            return $this->return_json(201, '', '查询失败');
        }
    }

    /*
     * 气费记录数
     */
    public function indexNum(Request $request, GasFee $gasFee)
    {
        $communityId = $request->input('community_id', 1);
        if (isset($request->condition) && !empty($request->condition)) {
            $condition = $request->condition;
            $query = 'SELECT COUNT(*) as count FROM `gas_fee` WHERE `community_id`=?and `status` = 1 and CONCAT(`house_number`,`username`) LIKE ?';
            $param = '%' . $condition . '%';
            $count = DB::select($query, array($communityId, $param));
            $res = $count[0]->count;
        } else {
            $res = $gasFee->where('status', 1)->count();
        }
        if ($res) {
            return $this->return_json(200, $res, '查询成功');
        } else {
            return $this->return_json(201, '', '查询失败');
        }
    }

    public function delete(Request $request, GasFee $gasFee)
    {
        $this->validate($request, [
            'id' => 'required'
        ], [
            'id.required' => 'id不能为空'
        ]);
        $param = $request->all();
        $param['status'] = 0;
        $code = $gasFee->whereid($request->id)->update($param);
        if ($code) {
            return $this->return_json(200, '', '删除成功');
        } else {
            return $this->return_json(201, '', '删除失败');
        }
    }

}
